#include "test_pm.h"

#include "string.h"

// thread_sleep_wakeup_cb test_suspend_cb(void *ctx)
// {
// #ifndef CM_OPENCPU_MODEL_POC 
//     cm_net_register_stop(); //如果注册了网络
// #endif
//     cm_demo_printf("enter sleep\n");
// }

// thread_sleep_wakeup_cb test_resume_cb(void *ctx)
// {
// #ifndef CM_OPENCPU_MODEL_POC 
//     cm_net_register_start(); //如果注册了网络
// #endif
//     cm_demo_printf("wake up\n");
// }

static int sleep_state = -1;
static int sleep_enter = -1;
static int sleep_exit = -1;
void  test_pm_enter(void)
{
    //不可执行打印，耗时操作
    //cm_demo_printf("enter pm.\n");
    sleep_state = 1;
    sleep_enter += 1;
}

void  test_pm_exit(void)
{
    //不可执行打印，耗时操作
    //cm_demo_printf("exit pm.\n");
    sleep_state = 0;
    sleep_exit += 1;
}
void test_powerkey_callback(void)
{
    cm_demo_printf("powerkey short press\n");
}
void cm_test_pm_operation(unsigned char *cmd[],int len)
{
    if(0 == strncmp("POWERREASON",cmd[2],strlen("POWERREASON")))
    {
        int32_t pm_get_power_on_reason = cm_pm_get_power_on_reason();
        cm_demo_printf("cm_pm_get_power_on_reason ret:%d\n",pm_get_power_on_reason);
        OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,pm_get_power_on_reason,cm_pm_get_power_on_reason);
    }else if(0 == strncmp("SET_PM_MODE",cmd[2],strlen("SET_PM_MODE")))
    {
        // int32_t pm_mode = -1;
        // if(0 == strncmp("MMC_1V8",cmd[3],strlen("MMC_1V8")))
        // {
        //     pm_mode = CM_PM_MODE_MMC_1V8;
        // }else if(0 == strncmp("MMC_3V2",cmd[3],strlen("MMC_3V2")))
        // {
        //     pm_mode = CM_PM_MODE_MMC_3V2;
        // }else if(0 == strncmp("LCD_1V8",cmd[3],strlen("LCD_1V8")))
        // {
        //     pm_mode = CM_PM_MODE_LCD_1V8;
        // }else if(0 == strncmp("LCD_3V2",cmd[3],strlen("LCD_3V2")))
        // {
        //     pm_mode = CM_PM_MODE_LCD_3V2;
        // }else{
        //     cm_demo_printf("param error!\n");
        // }
        int32_t pm_mode =atoi(cmd[3]);
        cm_demo_printf("cm_pm_set_mode ret:%d\n",cm_pm_set_mode(pm_mode));
    }else if(0 == strncmp("GET_PM_MODE",cmd[2],strlen("GET_PM_MODE")))
    {
        int32_t pm_mode = -1;
        cm_demo_printf("cm_pm_get_mode ret:%d\n",cm_pm_get_mode(&pm_mode));
        cm_demo_printf("pm_mode:%d\n",pm_mode);
    }else if(0 == strncmp("PM_INIT",cmd[2],strlen("PM_INIT")))
    {
        // cm_pm_cfg_t pm_cfg = {.cb_enter = test_pm_enter,
        //                     .cb_exit = test_pm_exit,
        //                     .cm_pm_cfg_nv = atoi(cmd[3]),
        //                     .cm_pm_lp_eanble = atoi(cmd[4])};
        cm_pm_cfg_t pm_cfg = {.cb_enter = test_pm_enter,
                    .cb_exit = test_pm_exit};
        cm_pm_init(pm_cfg);
        cm_demo_printf("cm_pm_init no ret.\n");
    }
    else if(0 == strncmp("SLEEP_LOCK",cmd[2],strlen("SLEEP_LOCK")))
    {
        cm_pm_work_lock();
        cm_demo_printf("cm_pm_work_lock no return\n");
    }else if(0 == strncmp("SLEEP_UNLOCK",cmd[2],strlen("SLEEP_UNLOCK")))
    {
        cm_pm_work_unlock();
        cm_demo_printf("cm_pm_work_unlock no return\n");
    }
    else if(0 == strncmp("REBOOT",cmd[2],strlen("REBOOT")))
    {
        // uint8_t type = atoi(cmd[3]);
        // cm_pm_reboot(type);
        cm_pm_reboot();
        cm_demo_printf("cm_pm_reboot no return\n");
    }else if(0 == strncmp("POWER_OFF",cmd[2],strlen("POWER_OFF")))
    {
        cm_pm_poweroff();
        cm_demo_printf("cm_pm_poweroff no return\n");
    }
    else if (0 == strncmp("SLEEP_STATE",cmd[2],strlen("SLEEP_STATE")))
    {
        cm_demo_printf("sleep_state:%d, sleep_enter:%d, sleep_exit:%d\n",sleep_state,sleep_enter,sleep_exit);
        sleep_state = -1;
        sleep_enter = -1;
        sleep_exit = -1;
    }
    else if (0 == strncmp("PWRK_CB",cmd[2],strlen("PWRK_CB")))
    {
        int32_t pm_powerkey_regist_callback = cm_pm_powerkey_regist_callback(test_powerkey_callback);
        cm_demo_printf("cm_pm_powerkey_regist_callback ret:%d\n",pm_powerkey_regist_callback);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,pm_powerkey_regist_callback,cm_pm_powerkey_regist_callback);
    }
    else{
        cm_demo_printf("param error!\n");
    }
}